<template>
	<div class="mainbody">
		<div class="location">
			<el-breadcrumb separator-class="el-icon-arrow-right">
				<el-breadcrumb-item>
					<i class="iconfont icon-home"></i>
					管理中心
				</el-breadcrumb-item>
				<el-breadcrumb-item>内容管理</el-breadcrumb-item>
				<el-breadcrumb-item>标签列表</el-breadcrumb-item>
			</el-breadcrumb>
		</div>

		<div class="toolbar-box">
			<div class="list-box">
				<div class="l-list">
					<el-button-group>
						<el-button icon="el-icon-plus" @click="$common.linkUrl('/article/label/edit/'+channelId)">新增</el-button>
						<el-button icon="el-icon-delete" @click="deleteCheckAll">删除</el-button>
					</el-button-group>
				</div>
				<div class="r-list">
					<div class="search-box">
						<el-input placeholder="输入关健字" v-model="keyword" @keyup.enter.native="initData" clearable>
							<el-button slot="append" icon="el-icon-search" @click="initData"></el-button>
						</el-input>
					</div>
				</div>
			</div>

		</div>

		<div class="content-box">
			<el-card class="table-card">
				<el-table ref="multipleTable" v-loading="loading" :data="listData" stripe class="table-list" @selection-change="handleSelectionChange">
					<el-table-column type="selection" width="45"></el-table-column>
					<el-table-column prop="id" label="序号" width="80"></el-table-column>
					<el-table-column prop="title" label="标题名称" min-width="120"></el-table-column>
					<el-table-column prop="addTime" label="创建时间" width="160"></el-table-column>
					<el-table-column label="排序" width="120">
						<template slot-scope="scope">
							<el-input-number size="mini" v-model="scope.row.sortId" @change="editSortId(scope.row.id, scope.row.sortId)" :min="-99999999" :max="99999999"></el-input-number>
						</template>
					</el-table-column>
					<el-table-column label="启用" width="90" align="center">
						<template slot-scope="scope">
							<el-switch :active-value="0" :inactive-value="1" v-model="scope.row.status" @change="editStatus(scope.row.id, scope.row.status)"></el-switch>
						</template>
					</el-table-column>
					<el-table-column fixed="right" label="操作" width="90">
						<template slot-scope="scope">
							<el-button size="mini" icon="el-icon-edit" @click="$common.linkUrl('/article/label/edit/'+channelId+'/'+scope.row.id)"></el-button>
							<el-button size="mini" type="danger" icon="el-icon-delete" @click="deleteItem(scope.row.id)"></el-button>
						</template>
					</el-table-column>
				</el-table>
			</el-card>

			<div class="pager-box">
				<el-pagination background
					@size-change="handleSizeChange"
					@current-change="handleCurrentChange"
					:current-page="pageIndex"
					:page-sizes="[10, 20, 50, 100]"
					:page-size="pageSize"
					layout="total, sizes, prev, pager, next, jumper"
					:total="totalCount">
				</el-pagination>
			</div>
		</div>
	</div>
</template>

<script>
	export default {
		props: {
			channelId: 0
		},
		data() {
			return {
				loading: false,
				keyword: '',
				totalCount: 0,
				pageSize: 10,
				pageIndex: 1,
				listData: [],
				multipleSelection: []
			}
		},
		methods: {
			//加载数据
			initData() {
				let _this = this;
				let sendUrl = `/admin/article/label/${_this.channelId}?pageSize=${_this.pageSize}&pageIndex=${_this.pageIndex}`;
				if (_this.keyword.length > 0) {
					sendUrl += "&keyword=" + encodeURI(_this.keyword);
				}
				_this.$api.request({
					url: sendUrl,
					beforeSend() {
						_this.loading = true
					},
					success(res) {
						_this.listData = res.data;
						let pageInfo = JSON.parse(res.headers["x-pagination"]);
						_this.totalCount = pageInfo.totalCount;
						_this.pageSize = pageInfo.pageSize;
						_this.pageIndex = pageInfo.pageIndex;
					},
					error(err) {
						_this.listData = [];
					},
					complete() {
						_this.loading = false
					}
				});
			},
			//多选删除
			deleteCheckAll() {
				let _this = this; //当前页面
				//拿到选中的数据
				let list = _this.multipleSelection;
				//检查是否有选中
				if (!list.length) {
					_this.$message({ type: 'warning', message: '请选择要删除的记录！' });
					return false;
				}
				//执行删除操作
				_this.$confirm('确认要删除该记录吗？', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(function () {
					let delIds = [];//创建一个数组
					list.forEach((item, index) => {
						//遍历源数据
						_this.listData.forEach((v, i) => {
							if (item.id == v.id) {
								delIds.push(item.id);
							}
						})
					});
					_this.$api.request({
						method: 'delete',
						url: `/admin/article/label/${_this.channelId}?ids=${delIds.toString()}`,
						loading: true,
						successMsg: '已删除成功',
						success(res) {
							_this.initData(); //重新加载列表
						}
					})
				}).catch(function () { });
			},
			//单项删除
			deleteItem(val) {
				let _this = this; //当前页面
				//执行删除操作
				_this.$confirm('确认要删除该记录吗？', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(function () {
					_this.$api.request({
						method: 'delete',
						url: `/admin/article/label/${_this.channelId}/${val}`,
						loading: true,
						successMsg: '已删除成功',
						success(res) {
							_this.initData();//重新加载列表
						}
					});

				}).catch(function () { });

			},
			//启用禁用操作
			editStatus(id, status) {
				this.$api.request({
					method: 'patch',
					url: `/admin/article/label/${this.channelId}/${id}`,
					data: [{ "op": "replace", "path": "/status", "value": status }],
					success(res) { }
				});
			},
			//排序数字操作
			editSortId(id, sortId) {
				this.$api.request({
					method: 'patch',
					url: `/admin/article/label/${this.channelId}/${id}`,
					data: [{ "op": "replace", "path": "/sortId", "value": sortId }],
					success(res) { }
				});
			},
			//选中第几行
			handleSelectionChange(val) {
				this.multipleSelection = val;
			},
			//每页显示数量
			handleSizeChange(val) {
				if (this.pageSize != val) {
					this.pageSize = val;
					this.initData();
				}
			},
			//跳转到第几页
			handleCurrentChange(val) {
				if (this.pageIndex != val) {
					this.pageIndex = val;
					this.initData();
				}
			}
		},
		created() {
			//加载数据
			this.initData();
		}
	}
</script>

<style lang="scss">
</style>